class rstruct:
    def __init__(self,items,name):
        self.items = items
        self.name = name
        self.pname = "P"+name
    def rust(self):
        ll = [len(x.name) for x in self.items]
        ident = max(ll)
        header = 'struct {}'.format(self.name)
        print(header,'{')
        for i in self.items:
            print("\t",end='')
            i.rust(ident)
        print("}")

class type_converter:
    tyconv = {'int8_t':'i8'
        , 'int16_t':'i16'
        , 'int32_t':'i32'
        , 'uint8_t':'u8'
        , 'uint16_t':'u16'
        , 'uint32_t':'u32'
        , 'float': 'f32'
    }
    defines = []
    @staticmethod
    def type_rename(ty):
        if ty in type_converter.tyconv:
            return type_converter.tyconv[ty]
        else:
            return ty
    @staticmethod
    def rust():
        ll = [len(x.c) for x in type_converter.defines]
        ident = max(ll)
        for c in type_converter.defines:
            c.rust(ident)
    @staticmethod
    def trim(s):
        while len(s) > 0 and s[-1] == ' ':
            s=s[:-1]
        return s
    
class item:
    def __init__(self,v):
        self.name = v['name']
        ty = type_converter.type_rename(v['raw_type'])
        if v['pointer'] == 1:
            ty = 'Box<{}>'.format(ty)
        if 'array_size' in v:
            self.ty = '[{};{}]'.format(ty,v['array_size'])
        else:
            self.ty = ty

    def rust(self,ident):
        space = " "*256
        line = '{}:{},'.format(self.name+space[:ident+1-len(self.name)],self.ty)
        print(line)

class constant:
    def __init__(self,s):
        self.c = s.split(' ')[0]
        left = s[len(self.c):]
        while len(left) > 0 and left[0]==' ':
            left = left[1:]
        self.a = left.split('//')[0]
        self.comment = left[len(self.a):]
        self.a = type_converter.trim(self.a)
    def rust(self,ident):
        space = " "*256
        line = 'const {} = {};{}'.format(self.c+space[:ident+1-len(self.c)],self.a,self.comment)
        if self.a != "":
            print(line)

def define(s):
    type_converter.defines.append(constant(s))
def alias(s):
    
    ty = s.Vars[0]['property_of_class']
    aty = s['raw_type']
    if aty[-1] == ',':
        aty = aty[:-1]
    # type_converter.tyconv[ty] = aty
    type_converter.tyconv[s['name']] = 'Box<{}>'.format(aty)    
    # print(ty,aty,s['name'])
def from_struct(classes):
    items = []
    for i in classes['properties']['public']:
        items.append(item(i))
    return rstruct(items,type_converter.type_rename(classes['name']))
